<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
use \Common\Model\MemberRegister;
use \Common\Model\MemberLogin;

//测试控制器
class MemberController extends Controller{
    //登录
    public function index(){    	
    	if(IS_AJAX){
			$userModel = new MemberLogin;
			if(!$userModel->store()){
				$this->ajax(['status'=>false,'message'=>$userModel->getError()]);								
			}else{
				$account = q('post.account');
				//获取上次登录时间
				$oldLoginTime = $userModel->where('account',$account)->pluck('loginTime');
				//获取当前登录时间
				$LoginTime = time();
				//更新admin表中登录时间
				$userModel->where('account',$account)->update(['loginTime'=>$LoginTime]);
				//存session信息	便于密码修改
				$_SESSION['user'] = [
					'account'=>$account,
					'uid'=>$userModel->where('account',$account)->pluck('uid'),
					'oldLoginTime'=>$oldLoginTime //上次登录时间
				];	
				$this->ajax(['status'=>true,'message'=>'登录成功']);
			}			
		}	
    	View::make();
    }
	
	//注册
	public function register(){
		//AJAx异步注册
		if(IS_AJAX){
			$userModel = new MemberRegister;
			if(!$userModel->store()){
				$this->ajax(['status'=>false,'message'=>$userModel->getError()]);								
			}else{
				$this->ajax(['status'=>true,'message'=>'注册成功']);
			}
		}
    	View::make();
    }
	
	//显示验证码
	public function code(){
		//num 验证码数量
		Code::num(1)->background('#FFFFFF')->height(32)->make();
	}
	
	//退出
	public function out(){
		unset($_SESSION['user']);
		//跳转到登陆页面
		go(U('Index/index'));
	}
}
